<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/logic/CustomCommand.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Place.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/Branch.php';
    require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/entity/BranchAddress.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/model/factory/DaoFactory.php';
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/FirePHPCore-0.3.2/lib/FirePHPCore/FirePHP.class.php';
		
	$debug = FirePHP :: getInstance(true);
	$debug -> setEnabled(true);
	
	//Permite eliminar los notices
	
	error_reporting (E_ALL ^ E_NOTICE);
	

	
	/*
	* Class to validate a credential
	*/
	class CommandCreateBranch extends CustomCommand {
		/*
		* Constructor of the class
		*/
		public function __construct ($pkRif,$name,$country, $stateProvince, $city, $zone, $startDate, $endDate)  {
		
		     /**
			 * Creating a Branch
			 */
			$this -> pkRif = $pkRif;
			$this -> name = $name;
				$GLOBALS['debug'] -> log($this -> pkRif, "rif--comando branch");
			    $GLOBALS['debug'] -> log($this -> name, "name");
			
			/**
			 * Creating an Branch
			 */
			 
		    $this -> Branch = new Branch($pkRif,$name);
			
		    /**
			 * Creating a place
			 */
			$this -> country = new Place("PAIS", $country);
			$this -> stateProvince = new Place("ESTADO/PROVINCIA", $stateProvince);
			$this -> city = new Place("CIUDAD", $city);
			$this -> zone = new Place("SECTOR", $zone);
				$GLOBALS['debug'] -> log($this -> country, "name");
			    $GLOBALS['debug'] -> log($this -> stateProvince, "name");			
			    $GLOBALS['debug'] -> log($this -> city, "name");
				$GLOBALS['debug'] -> log($this -> zone, "name");		
			 /**
			 * Branch_Address
			 */
			 
			   $GLOBALS['debug'] -> log($startDate, "fechainicial----comando branch");			
			    $GLOBALS['debug'] -> log($endDate, "fechafinal----comando branch");
				$GLOBALS['debug'] -> log($pkRif, "rif");
			$this -> branchAddress = new BranchAddress($startDate,$endDate,$pkRif);
			
		
	     
	
		
		}
		
		/*
		* Method to execute to validate the credential
		*/
		public function execute() {
				
			/**
			 * Creating a country
			 */	
			$command = CommandFactory :: createCommandCreatePlace($this -> country -> getType(), 
				$this -> country -> getName());
				$GLOBALS['debug'] -> log($this -> country, "pais----comando branch execute");
			
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a state/province
			 */
			$command = CommandFactory :: createCommandCreatePlace
				($this -> stateProvince -> getType(), $this -> stateProvince -> getName());
			$GLOBALS['debug'] -> log($this -> stateProvince, "provincia----comando branch execute");
			
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a city
			 */	
			$command = CommandFactory :: createCommandCreatePlace($this -> city -> getType(), 
				$this -> city -> getName());
				$GLOBALS['debug'] -> log($this -> city , "ciudad----comando branch execute");
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
			/**
			 * Creating a zone
			 */
			$command = CommandFactory :: createCommandCreatePlace($this -> zone -> getType(), 
				$this -> zone -> getName());
					$GLOBALS['debug'] -> log($this -> zone , "zone----comando branch execute");
			$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
				
			/**
			 * Creating Branch
			 */
			$daoFactory= DaoFactory:: getMySqlDaoFactory();
			$iuad= $daoFactory-> getMySqlBranchAccessDao();			
			$auth=$iuad->createBranch($this -> pkRif, $this -> name);
			$this -> result = $auth;
			$GLOBALS['debug'] -> log($this -> result, "fin de insercion de branch");
					
			/**
			 * Creating BranchAddress
			 */
			 $GLOBALS['debug'] -> log($this -> branchAddress -> getPkStartDate(), "fecha ini----comando branch execute");

				$command = CommandFactory :: createCommandCreateBranchAddress($this -> branchAddress -> getPkStartDate() , 
				$this -> branchAddress -> getEndDate(),$this -> branchAddress -> getPkFkRifBranch());
					
					$GLOBALS['debug'] -> log($this -> branchAddress -> getPkStartDate(), "fecha ini----comando branch execute");
					$GLOBALS['debug'] -> log($this -> branchAddress -> getEndDate() , "fecha f----comando branch execute");
					$GLOBALS['debug'] -> log($this -> branchAddress -> getPkFkRifBranch() , "rf----comando branch execute");
			    
				$command -> execute();
			
			if ($command -> getResult() == false) {
				$this -> result = $command -> getResult();
				return;
			}
			
	
		
			
	
		}
	}
?>